﻿using System;
using System.Reflection;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;

namespace CPN.PJN.Silverlight.Extensions.Converters
{
    /// <summary>
    /// Conversion d'une string en couleur ou couleur en string
    /// </summary>
    public class ColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is String)
            {
                if (value.ToString().StartsWith("#"))
                {
                    Color c = GetColorFromHex(value.ToString());
                    return new SolidColorBrush(c);
                }

                try
                {
                    Color c = GetColorFromString(value as string);
                    return new SolidColorBrush(c);
                }
                catch
                {
                    return null;
                }
            }
            else if (value is Color)
            {
                return new SolidColorBrush((Color)value);
            }

            return null;
        }

        private Color GetColorFromString(string colorString)
        {
            Type colorType = (typeof(Colors));
            if (colorType.GetProperty(colorString) != null)
            {
                object color = colorType.InvokeMember(colorString, BindingFlags.GetProperty, null, null, null);
                if (color != null)
                {
                    return (Color)color;
                }
            }
            else
            {
                try
                {
                    Line lne = (Line)XamlReader.Load("<Line xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Fill=\"" + colorString + "\" />");
                    return (Color)lne.Fill.GetValue(SolidColorBrush.ColorProperty);
                }
                catch { }
            }
            throw new InvalidCastException("Color not defined");
        }

        private Color GetColorFromHex(string s)
        {
            if (s.StartsWith("#"))
            {
                s = s.Substring(1);
            }
            byte a = System.Convert.ToByte(s.Substring(0, 2), 16);
            byte r = System.Convert.ToByte(s.Substring(2, 2), 16);
            byte g = System.Convert.ToByte(s.Substring(4, 2), 16);
            byte b = System.Convert.ToByte(s.Substring(6, 2), 16);
            return Color.FromArgb(a, r, g, b);
        }

        private string GetHexFromColor(Color c)
        {
            return string.Format("#{0}{1}{2}{3}",
                    c.A.ToString("X2"),
                    c.R.ToString("X2"),
                    c.G.ToString("X2"),
                    c.B.ToString("X2"));
        }


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
